package com.example.weight.navigation;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Point;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.bin.david.form.core.TableConfig;
import com.bin.david.form.data.CellInfo;
import com.bin.david.form.data.format.bg.BaseCellBackgroundFormat;
import com.bin.david.form.data.style.FontStyle;
import com.example.weight.R;
import com.example.weight.config.Config;
import com.example.weight.householdWaste.AbnormalRubbishActivity;
import com.example.weight.householdWaste.BottleClassifierPutrescibleActivity;
import com.example.weight.householdWaste.ChangeWaterActivity;
import com.example.weight.householdWaste.ChoseRoleActivity;
import com.example.weight.householdWaste.CombustiblePutrescibleActivity;
import com.example.weight.householdWaste.CombustiblePutrescibleRecordActivity;
import com.example.weight.householdWaste.DeodorantSprayTimeActivity;
import com.example.weight.householdWaste.ElectricRecordActivity;
import com.example.weight.householdWaste.FacilityMaintainPlanActivity;
import com.example.weight.householdWaste.FacilityRuntimeActivity;
import com.example.weight.householdWaste.FaultRepairActivity;
import com.example.weight.householdWaste.FaultRepairListActivity;
import com.example.weight.householdWaste.StoppageClearActivity;
import com.example.weight.householdWaste.WaterRecordActivity;
import com.example.weight.householdWaste.YijingStationResourceActivity;
import com.example.weight.model.FaultModel;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class HouseholdWasteNavActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_household_waste_nav);

        Intent previousIntent = getIntent();
        final String name = previousIntent.getStringExtra("name");
        final String factory = previousIntent.getStringExtra("factory");
        TitleBar titleBar = findViewById(R.id.household_waste_nav_titleBar);
        titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View v) {
                finish();
            }

            @Override
            public void onTitleClick(View v) {}

            @Override
            public void onRightClick(View v) {}
        });

        Button bottleClassifierPutrescibleNav, changeWaterNav, combustiblePutrescibleNav,abnormalRubbishNav,
                deodorantSprayTimeNav, electricRecordNav, facilityMaintainPlanNav, facilityRuntimeNav,
                faultRepairNav, stoppageClearNav, waterRecordNav, yijingStationResourceNav, combustiblePutrescibleRecordNav;

//        abnormalRubbishNav = findViewById(R.id.abnormal_rubbish_nav);
//        abnormalRubbishNav.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(HouseholdWasteNavActivity.this, AbnormalRubbishActivity.class);
//                intent.putExtra("name", name);
//                startActivity(intent);
//            }
//        });

        bottleClassifierPutrescibleNav = findViewById(R.id.bottle_classifier_putrescible_nav);
        bottleClassifierPutrescibleNav.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(HouseholdWasteNavActivity.this, BottleClassifierPutrescibleActivity.class);
                intent.putExtra("name", name);
                intent.putExtra("factory", factory);
                startActivity(intent);
            }
        });

        changeWaterNav = findViewById(R.id.change_water_nav);
        changeWaterNav.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(HouseholdWasteNavActivity.this, ChangeWaterNavActivity.class);
                intent.putExtra("name", name);
                intent.putExtra("factory", factory);
                startActivity(intent);
            }
        });

        combustiblePutrescibleNav = findViewById(R.id.combustible_putrescible_nav);
        combustiblePutrescibleNav.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(HouseholdWasteNavActivity.this, CombustiblePutrescibleActivity.class);
                intent.putExtra("name", name);
                intent.putExtra("factory", factory);
                startActivity(intent);
            }
        });

        combustiblePutrescibleRecordNav = findViewById(R.id.combustible_putrescible_record_nav);
        combustiblePutrescibleRecordNav.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(HouseholdWasteNavActivity.this, CombustiblePutrescibleRecordActivity.class);
                intent.putExtra("name", name);
                intent.putExtra("factory", factory);
                startActivity(intent);
            }
        });

        deodorantSprayTimeNav = findViewById(R.id.deodorant_spray_nav);
        deodorantSprayTimeNav.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(HouseholdWasteNavActivity.this, DeodorantSprayTimeActivity.class);
                intent.putExtra("name", name);
                intent.putExtra("factory", factory);
                startActivity(intent);
            }
        });

        electricRecordNav = findViewById(R.id.electric_record_nav);
        electricRecordNav.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(HouseholdWasteNavActivity.this, ElectricRecordActivity.class);
                intent.putExtra("name", name);
                intent.putExtra("factory", factory);
                startActivity(intent);
            }
        });

        facilityMaintainPlanNav = findViewById(R.id.facility_maintain_plan_nav);
        facilityMaintainPlanNav.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(HouseholdWasteNavActivity.this, ChoseRoleActivity.class);
                intent.putExtra("name", name);
                intent.putExtra("factory", factory);
                intent.putExtra("fName", "HouseholdWasteMaintainNavActivity");
                startActivity(intent);
            }
        });

        facilityRuntimeNav = findViewById(R.id.facility_runtime_nav);
        facilityRuntimeNav.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(HouseholdWasteNavActivity.this, FacilityRuntimeActivity.class);
                intent.putExtra("name", name);
                intent.putExtra("factory", factory);
                startActivity(intent);
            }
        });

        faultRepairNav = findViewById(R.id.fault_repair_nav);
        faultRepairNav.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RequestQueue queue = Volley.newRequestQueue(HouseholdWasteNavActivity.this);
                String url = Config.url + "/shlj/troubleshooting/getList" + "?factory=" + factory;
                StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                        new Response.Listener<String>() {
                            public void onResponse(String response) {
                                try {
                                    JSONObject jsonObject = new JSONObject(response);
                                    if (!jsonObject.isNull("data")) {
                                        String listString = "";
                                        JSONArray data = jsonObject.getJSONArray("data");
                                        for (int i = 0; i < data.length(); i++) {
                                            JSONObject item = data.getJSONObject(i);
                                            String id = item.getString("id");
                                            String system = item.getString("systemName");
                                            String equipment = item.getString("equipmentName");
                                            String occurTime = item.getString("timeOfFailure");
                                            String step = item.getString("stage");
                                            String description = item.getString("faultDescription");
                                            String personInCharge = item.getString("personInCharge");
                                            FaultModel faultModel = new FaultModel(id, system, equipment, occurTime, step, description, personInCharge);
                                            listString = listString.concat(faultModel.toSplitWithSpace());
                                            if (i < data.length()-1)
                                                listString = listString.concat(",");
                                        }

                                        Intent intent = new Intent(HouseholdWasteNavActivity.this, FaultRepairListActivity.class);
                                        intent.putExtra("name", name);
                                        intent.putExtra("factory", factory);
                                        intent.putExtra("list", listString);
                                        startActivity(intent);
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {}
                });
                stringRequest.setRetryPolicy(new DefaultRetryPolicy(10 * 1000,
                        DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
                queue.add(stringRequest);

//                Intent intent = new Intent(HouseholdWasteNavActivity.this, FaultRepairListActivity.class);
//                intent.putExtra("name", name);
//                startActivity(intent);
            }
        });

        stoppageClearNav = findViewById(R.id.stoppage_clear_nav);
        stoppageClearNav.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(HouseholdWasteNavActivity.this, StoppageClearActivity.class);
                intent.putExtra("name", name);
                intent.putExtra("factory", factory);
                startActivity(intent);
            }
        });

        waterRecordNav = findViewById(R.id.water_record_nav);
        waterRecordNav.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(HouseholdWasteNavActivity.this, WaterRecordActivity.class);
                intent.putExtra("name", name);
                intent.putExtra("factory", factory);
                startActivity(intent);
            }
        });

        yijingStationResourceNav = findViewById(R.id.yijing_station_resource_nav);
        yijingStationResourceNav.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(HouseholdWasteNavActivity.this, YijingStationResourceActivity.class);
                intent.putExtra("name", name);
                intent.putExtra("factory", factory);
                startActivity(intent);
            }
        });
    }

    public void turnBrowser(View view){
        Uri uri = Uri.parse("http://39.105.29.195/#/login");
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);
    }
}
